#include "chartshow.h"
#include "ui_chartshow.h"
#include "datareader.h"
QVector <double> chart_x, chart_y;
chartshow::chartshow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::chartshow)
{
    ui->setupUi(this);
    setWindowTitle(QString("显示曲线图"));//设置窗口的名称
#if QchartUse==UseQcplot
    showchart_useqcplot();
#else
    showchart();
#endif

}

chartshow::~chartshow()
{
    delete ui;
}
#if QchartUse==UseQcplot
void chartshow::showchart_useqcplot(void)
{
    //ui->showWidget->graph(0);
    ui->showWidget->addGraph()->setName("数据曲线图");
    ui->showWidget->addGraph()->addData(chart_x,chart_y);
    ui->showWidget->xAxis->setLabel("X");
    ui->showWidget->yAxis->setLabel("y");
    ui->showWidget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iMultiSelect | QCP::iSelectItems);
}
#else
void chartshow::showchart(void)
{    
    ui->showWidget->setRenderHint(QPainter::Antialiasing);
    ui->showWidget->chart()->setTitle(QStringLiteral("QSplineSeries演示"));
    ui->showWidget->chart()->setTheme(QChart::ChartThemeDark);
    //setCentralWidget(chartView);
//创建曲线序列
    QLineSeries *series0 = new QLineSeries();
    QLineSeries *series1 = new QLineSeries();
    series0->setName("Sin曲线");
    series1->setName("Cos曲线");
    ui->showWidget->chart()->addSeries(series0); //序列添加到图表
    ui->showWidget->chart()->addSeries(series1);

//序列添加数值
    qreal   t=0,y1,y2,intv=0.1;
    int cnt=100;
    for(int i=0;i<cnt;i++)
    {
        y1=qSin(t);//+qrand();
        series0->append(t,y1);

        y2=qSin(t+20);
        series1->append(t,y2);

        t+=intv;
    }


//    chart->createDefaultAxes();
//    chart->axisX()->setTitleText("time(secs)");
//    chart->axisY()->setTitleText("value");

//创建坐标轴
    QValueAxis *axisX = new QValueAxis; //X 轴
//    curAxis=axisX;
    axisX->setRange(0, 10); //设置坐标轴范围
//    axisX->setLabelFormat("%.1f"); //标签格式
//    axisX->setTickCount(11); //主分隔个数
//    axisX->setMinorTickCount(4);
    axisX->setTitleText("time(secs)"); //标题
//    axisX->setGridLineVisible(false);

    QValueAxis *axisY = new QValueAxis; //Y 轴
    axisY->setRange(-2, 2);
    axisY->setTitleText("value");
//    axisY->setTickCount(5);
//    axisY->setMinorTickCount(4);
//    axisY->setLabelFormat("%.2f"); //标签格式
//    axisY->setGridLineVisible(false);

    ui->showWidget->chart()->setAxisX(axisX, series0); //为序列设置坐标轴
    ui->showWidget->chart()->setAxisY(axisY, series0); //

    ui->showWidget->chart()->setAxisX(axisX, series1); //为序列设置坐标轴
    ui->showWidget->chart()->setAxisY(axisY, series1); //
}
#endif
